#include "graphClass.h"

graphClass::graphClass(const wstring filePathIN,COLORREF colorIN,HWND hWndIN)   
{
    hWnd = hWndIN;
    valid = false;        
    started = false;
    filePath = filePathIN;
        color = colorIN;
    values;
        maxValue = 0;
        minValue = 0;
        xScale = 1;
        yScale = 1;
        xOffset = 0;
        yOffset = 0;
        visible = true;
    input.open(filePath.c_str());                                                
    if (!input)
    {
        cerr << "Couldn't open file to read";
        input.close();
        return;
    }
    else
    {
        //check for errors (file emty, first line not number etc!!!!!!!! programmer notice: not a bug. Just don't use broken files :P
        input>>period;        //READ PERIOD FROM FILE HERE (first line in file is the period)
        valid = true;
        timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
        started = true;
    }
}
graphClass::graphClass(const graphClass &copy)    
{
    period = copy.period;
    filePath = copy.filePath;    
        color = copy.color;
    values;
    started = false;
    valid = copy.valid;
        xScale = copy.xScale;
        yScale = copy.yScale;
        xOffset = copy.xOffset;
        yOffset = copy.yOffset;
        maxValue = 0;
        minValue = 0;
        visible = copy.visible;
    if(valid)
    {
        input.open(filePath.c_str());
        input>>period;
        timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
        started = true;
    }
    ii;
    hWnd=copy.hWnd;
}

graphClass::~graphClass()
{
    if (started)
    {
        timeKillEvent(timer);
    }
    input.close();
}

void graphClass::start()
{
    timer = timeSetEvent(period, 0,timerProc,(DWORD_PTR)this,TIME_PERIODIC);
    started = true;
}
void graphClass::stop()
{
    timeKillEvent(timer);
    started = false;
}

void graphClass::sendMes()
{
        SendMessage(hWnd,(WM_USER+100),NULL,NULL);
}

int graphClass::getPeriod()
{
    return period;
}

bool graphClass::startReading()
{
    if(!(values.empty()))
    {
        ii = values.begin();
        return true;
    }
    else
    {
        return false;
    }
}

double graphClass::readNext()
{
    if (ii!=values.end())    //read until eof!!!
    {
        double res = *ii;
        ii++;
        return res;
    }
    else
    {
        return NULL;
    }
}

void graphClass::loadValue()
{
    double valueIN;
    if(!input.eof())
    {
    input >> valueIN;    //until eof
    values.push_back(valueIN);
        if (valueIN > maxValue)
                maxValue = valueIN;
        if (valueIN < minValue)
                minValue = valueIN;//check if it works with double properly
    sendMes();
    }
    else
    {
        this->stop();
    }
    //cerr << valueIN; -not needed anymore...never was in the first place, haha XD
    return;
}


int graphClass::getLength()
{
    return values.size();
}

void graphClass::setXScale(int xScaleIN)
{
        this->xScale = xScaleIN;
        sendMes();
}
void graphClass::setYScale(int yScaleIN)
{
        this->yScale = yScaleIN;
        sendMes();
}
void graphClass::setXOffset(int xOffsetIN)
{
        this->xOffset = xOffsetIN;
        sendMes();
}
void graphClass::setYOffset(int yOffsetIN)
{
        this->yOffset = yOffsetIN;
        sendMes();
}
int graphClass::getXScale()
{
        return this->xScale;
}
int graphClass::getYScale()
{
        return this->yScale;
}
int graphClass::getXOffset()
{
        return this->xOffset;
}
int graphClass::getYOffset()
{
        return this->yOffset;
}

double graphClass::getMax()
{
        double min = this->minValue*this->yScale+this->yOffset;
        double max = this->maxValue*this->yScale+this->yOffset;
        return (abs(min)>abs(max))?abs(min):abs(max);
}

double graphClass::getValue(int i)
{
        return this->values[i];
}

void graphClass::show()
{
        this->visible = true;
}

void graphClass::hide()
{
        this->visible = false;
}

bool graphClass::isVisible()
{
       return this->visible;
}

bool graphClass::isStarted()
{
      return this->started;
}


COLORREF graphClass::getColor()
{
      return this->color;
}

HWND graphClass::getHCtrl()
{
    return this->hCtrl;
}
void graphClass::setHCtrl(HWND hWnd)
{
    this->hCtrl=hWnd;
}

void CALLBACK timerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)    //make it belong to class?
{
        ((graphClass*)dwUser)->loadValue();        
}

